<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id: Page.class.php 2712 2012-02-06 10:12:49Z liu21st $

class MyPage {
 	private $total;		//总记录数
	private $page;		//当前页
	private $num;		//每页显示记录数
	private $pageNum;	//总页数
	private $offset;	//数据库查询的偏移量(查询开始的记录)
	private $prev_page;//当前的上一页码
	private $next_page;//当前的下一页码
	private $maxCtrlNum;//数字控制页码最多显示几个(不计算首页和末页在内)
	
	public $className;	//样式名
	public $prevText;	//上一页按扭显示文本
	public $nextText;	//下一页按扭显示文本
	
	/**
	 * 构造方法
	 * @param total int 总记录数
	 * @param num int 每页显示多少条记录
	 * @param maxCtrlNum 数字控制页码按扭最多显示几个
	 * @param page int 当前页码 默认取$_GET['page']
	 */
	public function __construct($total,$num=20,$maxCtrlNum=10,$page=null){
		//计算总页数
		$this->pageNum=ceil($total/$num);

		$this->total=$total;
		$this->num	=$num;
		$this->maxCtrlNum=$maxCtrlNum;
		
		//当前页码
		if(empty($page)) {$page=intval($_GET["page"]);}
		if($page<1) {$page=1;}
		if($page>$this->pageNum) {$page=$this->pageNum;}
		if($page<1) {$page=1;}
		$this->page	=$page;
		//数据库查询的偏移量   (当前页-1)*每页条数
		$this->offset = ($page-1) * $num;
		//计算上一页
		$this->prev_page = ($this->page<=1) ? 1 : $this-> page - 1;
		//计算下一页
		$this->next_page = ($this->page == $this->pageNum) ? $this->pageNum : $this->page + 1;
	}
	
	/**
	 * 页码相关信息
	 * 
	 */
	public function getPageInfo(){
		return array(
			"total"		=>$this->total,			//总记录数
			"page"		=>$this->page,			//当前页
			"num"		=>$this->num,			//每页显示记录数
			"pageNum"	=>$this->pageNum,		//总页数
			"prev_page"	=>$this->prev_page,		//当前的上一页码
			"next_page"	=>$this->next_page,		//当前的下一页码
			"offset"	=>$this->offset			//数据库查询的偏移量(查询开始的记录,常用于limit操作)
		);
	}
	
	/**
	 * 输出style样式表
	 */
	public function getPageStyle($cssFile=""){
		if($cssFile==""){$cssFile='./style/page_style.css';}
		$style='<link rel="stylesheet" type="text/css" href="'.$cssFile.'" media="all" />';
		return $style;
	}

	/**
	 * 输出控制翻页链接
	 * 
	 * @param className string 采用哪一种样式
	 * @param prevText string 上一页按扭显示文本 默认显示  "< Prev"
	 * @param nextText string 下一页按扭显示文本 默认显示  "Next >"
	 */
	public function getPageCtrl($className="digg",$prevText="&lt; Prev",$nextText="Next &gt;"){
		$this->className=$className;
		$this->prevText=$prevText;
		$this->nextText=$nextText;
	
		//跳转的链接(去除了page参数)
		$url=$this->getUrl();
		
		//当前页码的位置
		$cur=ceil($this->maxCtrlNum/10*3);
		
		//开始数字
		if($this->page <= $cur || $this->pageNum <= $this->maxCtrlNum){				
			$ctrl_begin=1;
		}else if($this->page > $this->pageNum - $this->maxCtrlNum){
			$ctrl_begin=$this->pageNum - $this->maxCtrlNum+1;
		}else{
			$ctrl_begin=$this->page-$cur;
		}
		
		//结束数字
		$ctrl_end=$ctrl_begin+$this->maxCtrlNum-1;
		
		//不能大于总页数
		if($ctrl_end>$this->pageNum){$ctrl_end=$this->pageNum;}
		
		$ctrl_num_html="";
		for($i=$ctrl_begin;$i<=$ctrl_end;$i++){
			if($i==$this->page){
				//如果是当前页，则不加超链接
				$ctrl_num_html.="<span class='current' >{$i}</span>";
			}else{
				$ctrl_num_html.="<a href='{$url}page={$i}'>{$i}</a>";
			}
		}
		
		//判断是否需要加上省略号(...)
		if($ctrl_begin != 1){
			$ctrl_num_html="<a href='{$url}page=1'><span>1</span></a><span>...</span>".$ctrl_num_html;
		}
		if($ctrl_end != $this->pageNum){
			$ctrl_num_html.="<span>...</span><a href='{$url}page={$this->pageNum}'><span>{$this->pageNum}</span></a>";
		}
		
		//上一页
		if($this->page==1){
			$prev="<span class='disabled'>{$this->prevText}</span>";
		}else{
			$prev="<a href='{$url}page={$this->prev_page}'>{$this->prevText}</a>";
		}
		
		//下一页
		if($this->page==$this->pageNum){
			$next="<span class='disabled'>{$this->nextText}</span>";
		}else{
			$next="<a href='{$url}page={$this->next_page}'>{$this->nextText}</a>";
		}
		
		//输出控制翻页链接
		//<div class="digg"><span class="disabled"> < </span><span class="current">1</span><a href="#">2</a><a href="#">3</a><a href="#">4</a><a href="#">5</a><a href="#">6</a><a href="#">7</a>...<a href="#">199</a><a href="#">200</a><a href="#"> > </a></div>
		$html ="<div class=\"{$this->className}\">";
		$html.=$prev;
		$html.=$ctrl_num_html;
		$html.=$next;
		$html.="</div>";
		return $html;
	}
	
	//当前url 去掉page信息
	private function getUrl(){
		//当前url
		$url=$_SERVER['PHP_SELF'];

		//从get参数中去掉page信息
		$geturl=array();
		foreach($_GET as $k=>$v){
			if($k!='page'){
				$geturl[]=$k."=".$v;
			}
		}
		//从新组装成url
		if(count($geturl)>0){
			$url.='?'.implode('&',$geturl).'&';
		}else{
			$url.='?';
		}
		return $url;
	}
}